package cn.net.yato.picture.mapper;

import cn.net.yato.picture.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName RoleMapper
 * @Description TODO
 * @Author yato
 * @Date 2020/2/17 14:41
 * @Version 1.0
 */
@Component
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 查询所有角色信息
     * @return
     */
    @Select("SELECT  role.id,role.role_id,role.`name`,role.perms,role.description," +
            "role.`status`,role.create_time,role.update_time  FROM  role")
    @Results(id = "roleResult", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "roleId", column = "role_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "permissions",column = "role_id",many = @Many(select = "cn.net.yato.picture.mapper.PermissionMapper.getPermissionsByRoleId"))
    })
    List<Role> getAllRoles();

    /**
     * 根据用户ID查询所拥有的角色信息
     * @param userId
     * @return
     */
    @Select("SELECT  role.id,role.role_id,role.`name`,role.perms,role.description," +
            "role.`status`,role.create_time,role.update_time  FROM  user_role ," +
            "role  WHERE  user_role.user_id = #{userId}  AND  " +
            "role.role_id = user_role.role_id")
    @ResultMap(value = "roleResult")
    List<Role> getRolesByUserId(@Param(value = "userId") String userId);
}
